package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    private final SetmealMapper setmealMapper;
    private final SetmealDishMapper setmealDishMapper;
    private final CategoryMapper categoryMapper;

    @Autowired
    public SetmealServiceImpl(SetmealMapper setmealMapper, SetmealDishMapper setmealDishMapper, CategoryMapper categoryMapper){
        this.setmealMapper = setmealMapper;
        this.setmealDishMapper = setmealDishMapper;
        this.categoryMapper = categoryMapper;
    }

    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        this.setmealMapper.insert(setmeal);

        Long id = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
                this.setmealDishMapper.insert(setmealDish);
            });
        }

    }

    @Override
    public SetmealVO getById(Long id){
        Setmeal setmeal = this.setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = this.setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        try (Page<SetmealVO> page = this.setmealMapper.pageQuery(setmealPageQueryDTO)) {
            List<SetmealVO> SetmealVOS = page.getResult();
            SetmealVOS.forEach(setmealVO -> {
                Long category_id = setmealVO.getCategoryId();
                Category category = this.categoryMapper.getById(category_id);
                if (category != null){
                    setmealVO.setCategoryName(category.getName());
                }
            });
            return new PageResult(page.getTotal(), SetmealVOS);
        }
    }

    @Override
    public void setStatus(int status, Long id){
        this.setmealMapper.setStatus(status, id);
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO){
        Long id = setmealDTO.getId();
        // 删除菜品
        this.setmealDishMapper.deleteBySetmealId(id);

        // 新增菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
                this.setmealDishMapper.insert(setmealDish);
            });
        }

        // 修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        this.setmealMapper.update(setmeal);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids){
        this.setmealMapper.delete(ids);
        ids.forEach(this.setmealDishMapper::deleteBySetmealId);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
